/**
 *
 *搜索二叉树
 * 模拟实现
 *
 */

public class BinarySearchTree {

    static class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int key) {
            this.key = key;
        }
    }

    public TreeNode root;

    /**
     * 插入一个元素
     * @param key
     */
    public boolean insert(int key) {
        TreeNode node = new TreeNode(key);
        if (root == null) {
            root = node;
            return true;
        }
        TreeNode cur = root;
        TreeNode curP = root;
        while (cur != null) {
            if (cur.key > key) {
                curP = cur;
                cur = cur.left;
            }else if (cur.key < key) {
                curP = cur;
                cur = cur.right;
            }else {
                System.out.println("该元素已存在");
                return true;
            }
        }
        if (key < curP.key) {
            curP.left = node;
        }
        if (key > curP.key) {
            curP.right = node;
        }
        return true;
    }
    //查找key是否存在
    public TreeNode search(int key) {
        if (root == null) {
            return null;
        }
        TreeNode cur = root;
        while (cur != null) {
            if (cur.key > key) {
                cur = cur.left;
            }else if (cur.key < key) {
                cur = cur.right;
            }else {
                return cur;
            }
        }
        return null;
    }
    //删除key的值
    public boolean remove(int key) {
        if (root == null) {
            return false;
        }
        TreeNode cur = root;
        TreeNode curP = root;
        while (cur != null) {
            if (cur.key > key) {
                cur = cur.left;
            }else if (cur.key < key) {
                cur = cur.right;
            }else {
                removeNode(cur,curP);
            }
        }
        return false;
    }

    private void removeNode(TreeNode cur, TreeNode curP) {
        if (cur.left == null) {//cur的左树为空
            //cur是根节点
            if (cur == root) {
                root = root.right;
                return;
            }
            //cur不是根节点
            if (cur == curP.left) {//cur是其父节点的左子树
                curP.left = cur.right;
                return;
            }else {//cur是其父节点的右子树
                curP.right = cur.right;
                return;
            }
        } else if (cur.right == null) {
            if (cur == root) {
                root = root.left;
                return;
            }
            if (cur == curP.left) {
                curP.left = cur.left;
                return;
            }else {
                curP.left = cur.left;
                return;
            }
        }else {
            //cur的左右子树都不为空
            TreeNode t = cur.left;
            TreeNode tParent = cur;
            //找cur左树的最右边
            //或者找cur右树的最左边
            //这里找的是cur左树的最右边
            while (t.right != null) {
                tParent = t;
                t = t.right;
            }
            cur.key = t.key;

            //cur的左右子树可能有一层 也可能有多层
            if (tParent.right == t) {
                tParent.right = t.left;
            }else {
                tParent.left = t.left;
            }
        }

    }

}